package com.lyh.hkcloud.module.m1300089.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.module.m1300089.IndexActivity;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.StringUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description: 数据适配器
 * @company—date: 利元数据 2022-02-14 15:12
 **/

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String,Object>> dataList;
    //private int saveposition;
    //private DeleteCallback Callback;
    private IndexActivity mContext;

    /**
     * 构造器
     *
     * @param context  上下文对象
     * @param dataList 数据
     */
    public ListViewAdapter(Context context, List<HashMap<String,Object>> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_listview_item_1300089,parent,false);
            holder = new ViewHolder();
            holder.et_inventoryNo = (TextView) convertView.findViewById(R.id.tv_inventoryNo);
            holder.et_inventoryCycleName = (TextView) convertView.findViewById(R.id.tv_inventoryCycleName);
            holder.et_inventoryUserName = (TextView) convertView.findViewById(R.id.tv_inventoryUserName);
            holder.et_memo = (TextView) convertView.findViewById(R.id.tv_memo);
            holder.et_inventoryStatusName = (TextView) convertView.findViewById(R.id.tv_inventoryStatusName);
            holder.et_inventoryTime = (TextView) convertView.findViewById(R.id.tv_inventoryTime);
            holder.bt_check = (Button) convertView.findViewById(R.id.bt_check);
            holder.bt_detail=(Button) convertView.findViewById(R.id.bt_detail);
            //holder.ll_details = convertView.findViewById(R.id.ll_details);

            convertView.setTag(holder);   //将Holder存储到convertView中
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        Object inventoryPlanId=dataList.get(position).get(BusinessConstants.ID);
        Object inventoryNo=dataList.get(position).get(BusinessConstants.INVENTORY_NO);
        Object inventoryTime=dataList.get(position).get(BusinessConstants.INVENTORY_TIME);
        Object inventoryCycleName=dataList.get(position).get(BusinessConstants.INVENTORY_CYCLE_NAME);
        Object inventoryUserName=dataList.get(position).get(BusinessConstants.INVENTORY_USER_NAME);
        Object memo=dataList.get(position).get(BusinessConstants.MEMO);
        Object inventoryStatusName=dataList.get(position).get(BusinessConstants.INVENTORY_STATUS_NAME);
        Object inventoryStatusCode=dataList.get(position).get(BusinessConstants.INVENTORY_STATUS_CODE);
        Object detailTotalNum=dataList.get(position).get(BusinessConstants.DETAIL_TOTAL_NUM);
        Object detailScanNum=dataList.get(position).get(BusinessConstants.DETAIL_SCAN_NUM);
        holder.et_inventoryNo.setText(getValue(inventoryNo));
        holder.et_inventoryCycleName.setText(getValue(inventoryCycleName));
        holder.et_inventoryUserName.setText(getValue(inventoryUserName));
        holder.et_memo.setText(getValue(memo));
        holder.et_inventoryStatusName.setText(getValue(inventoryStatusName));
        holder.et_inventoryTime.setText(getValue(inventoryTime));
        Object scanNum=StringUtils.isEmpty(detailScanNum.toString())?0:detailScanNum;
        Object totalNum=StringUtils.isEmpty(detailTotalNum.toString())?0:detailTotalNum;
        holder.bt_detail.setText("明  细("+scanNum+"/"+totalNum+")");
        if("complete".equals(getValue(inventoryStatusCode))||"cancel".equals(getValue(inventoryStatusCode))){
            holder.bt_check.setBackgroundColor(Color.parseColor("#C8C9CC"));
            holder.bt_check.setEnabled(false);
        }else{
            holder.bt_check.setBackgroundColor(Color.parseColor("#0080FF"));
            holder.bt_check.setEnabled(true);
        }
        holder.bt_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, com.lyh.hkcloud.module.m1300090.IndexActivity.class);
                Map<String, Object> inventoryMap = new HashMap<>();
                inventoryMap.put(BusinessConstants.ID,inventoryPlanId);
                inventoryMap.put(BusinessConstants.INVENTORY_NO,inventoryNo);
                inventoryMap.put(BusinessConstants.INVENTORY_TIME,inventoryTime);
                inventoryMap.put(BusinessConstants.SCAN_RESULT_CODE,BusinessConstants.SCANNED);
                inventoryMap.put(BusinessConstants.INVENTORY_USER_NAME,inventoryUserName);
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(inventoryMap).toString());
                context.startActivity(intent);
            }
        });
        holder.bt_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, com.lyh.hkcloud.module.m1300091.IndexActivity.class);
                Map<String, Object> inventoryMap = new HashMap<>();
                inventoryMap.put(BusinessConstants.ID,inventoryPlanId);
                inventoryMap.put(BusinessConstants.SCAN_RESULT_CODE,BusinessConstants.SCANNED);
                /*inventoryMap.put(BusinessConstants.INVENTORY_NO,inventoryNo);
                inventoryMap.put(BusinessConstants.INVENTORY_TIME,inventoryTime);
                inventoryMap.put(BusinessConstants.INVENTORY_USER_NAME,inventoryUserName);*/
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(inventoryMap).toString());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    /**
     * ViewHolder类
     */
    private final class ViewHolder {
        //盘点单号
        TextView et_inventoryNo;
        //盘点周期
        TextView et_inventoryCycleName;
        //盘点人员
        TextView et_inventoryUserName;
        //备注
        TextView et_memo;
        //盘点状态
        TextView et_inventoryStatusName;
        //盘点时间
        TextView et_inventoryTime;
        //盘点扫描
        Button bt_check;
        //明细
        Button bt_detail;

        //LinearLayout ll_details;

    }

    private String getValue(Object value){
        if("null".equals(value)||"".equals(value)){
            return "";
        }
        return value.toString();
    }
}

